﻿<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction
  Description="Copy Item preserving field values"
  Title="Copy Item"
  Id="{b204e0c2-685d-450b-9450-fdb4ab9e3c5c}"
  Location="CommandUI.Ribbon.ListView"
  RegistrationId="100"
  RegistrationType="List"
  Sequence="100"
  Rights="EditListItems"
  xmlns="http://schemas.microsoft.com/sharepoint/">
    <CommandUIExtension xmlns="http://schemas.microsoft.com/sharepoint/">
      <!-- Define the (UI) button to be used for this custom action -->
      <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.ListItem.Actions.Controls._children">
          <Button Id="{ac5bc1a8-6b23-4bdf-be7a-fa7a601e15c8}"
          Command="{9637008c-f86a-49d6-bbc7-cf331dbb7fac}"
          Image32by32="~site/_layouts/Images/SPRA/CopyItemActionRibbon32x32.png"
          Image16by16="~site/_layouts/Images/SPRA/CopyItemActionRibbon32x32.png"
          Sequence="100"
          LabelText="Copy Item"
          Description="Copy Item perserving field values"
          TemplateAlias="o1" />
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler
          Command="{9637008c-f86a-49d6-bbc7-cf331dbb7fac}"
          CommandAction="javascript:
              function DialogCallback(dialogResult, returnValue){
                  SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
              }
              
              var ctx = SP.ClientContext.get_current(); 
              var items = SP.ListOperation.Selection.getSelectedItems(ctx); 
              var selectedItems = ''; 
              var i; 
              for (i in items) 
              { 
                  selectedItems += '|' + items[i].id; 
              }
              var options = { url:&quot;{SiteUrl}/_layouts/SPRA/CopyItem.aspx?ID={ItemId}&amp;List={ListId}&amp;SelectedItems=&quot;+selectedItems+&quot;&amp;Source=&quot; + window.location,
                              allowMaximize: true,
                              showClose: false,
                              dialogReturnValueCallback: DialogCallback
                            };
              SP.UI.ModalDialog.showModalDialog(options);"
          EnabledScript="javascript:
              function enableCopyItemButton() {
                var ctx = SP.ClientContext.get_current(); 
                var items = SP.ListOperation.Selection.getSelectedItems(ctx);
                
                return (items != null &amp;&amp; items.length == 1);
              }
              enableCopyItemButton();"
          />
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>
  <CustomAction
  Description="Copy Item preserving field values"
  Title="Copy Item"
  Id="{b204e0c2-685d-450b-9450-fdb4ab9e3c5c}"
  Location="CommandUI.Ribbon.DisplayForm"
  RegistrationId="100"
  RegistrationType="List"
  Sequence="100"
  Rights="EditListItems"
  xmlns="http://schemas.microsoft.com/sharepoint/">
    <CommandUIExtension xmlns="http://schemas.microsoft.com/sharepoint/">
      <!-- Define the (UI) button to be used for this custom action -->
      <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.ListForm.Display.Manage.Controls._children">
          <Button Id="{6f82835d-b77a-427e-a616-a42e35890be4}"
          Command="{4dbbd280-552c-4eed-9168-63a5d92248bc}"
          Image32by32="~site/_layouts/Images/SPRA/CopyItemActionRibbon32x32.png"
          Image16by16="~site/_layouts/Images/SPRA/CopyItemActionRibbon32x32.png"
          Sequence="11"
          LabelText="Copy Item"
          Description="Copy Item perserving field values"
          TemplateAlias="o1" />
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler
          Command="{4dbbd280-552c-4eed-9168-63a5d92248bc}"
          CommandAction="javascript:
              function DialogCallback(dialogResult, returnValue){
                  SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
              }
              
              var ctx = SP.ClientContext.get_current(); 
              var items = SP.ListOperation.Selection.getSelectedItems(ctx); 
              var selectedItems = ''; 
              var i; 
              for (i in items) 
              { 
                  selectedItems += '|' + items[i].id; 
              }
              var options = { url:&quot;{SiteUrl}/_layouts/SPRA/CopyItem.aspx?ID={ItemId}&amp;List={ListId}&amp;SelectedItems=&quot;+selectedItems+&quot;&amp;Source=&quot; + window.location,
                              allowMaximize: true,
                              showClose: false,
                              dialogReturnValueCallback: DialogCallback
                            };
              SP.UI.ModalDialog.showModalDialog(options);"
          />
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>
</Elements>